<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%!
	public void query(List<Map> result, List<Map> surveys) {
		if (isEmpty(surveys)) {
			return;
		}
		String index = "-1";
		for (int i = 0; i < surveys.size(); i++) {
			Map map = surveys.get(i);
			index += "," + Tools.getDesc(map.get("category_id"), "-1");
		}
		surveys = QBuilder.db("basedb").select("new map(id as category_id, parent_id as parent_id, title as title, sub_title as detail)").from("visit_category").where(new Where("parent_id in("+ index +")").and("status = ", STATUS_ENABLE)).query().getResultObj();
		result.addAll(surveys);
		query(result, surveys);
		return;
	}
%>
<%
	User user = userByUid(token.getUid());
	Integer deptId = user.getDeptId();
    List<Map> surveys = QBuilder.db("basedb").select("new map(vc.id as category_id, vc.title as title, vc.sub_title as detail, vh.mark as mark)").from("visit_category vc, visit_hospital vh").where(new Where("vh.category = vc.id").and("vh.status = ", STATUS_ENABLE).and("vc.status = ", STATUS_ENABLE).and("vh.hospital = ", deptId)).query().getResultObj();
    if (isEmpty(surveys)) {
        msg = new Message(30041, "暂无随访模板");
    }
	for (int i = 0; i < surveys.size(); i++) {
		Map map = surveys.get(i);
		String id = Tools.getDesc(map.get("category_id"), "-1");
		String mark = Tools.getDesc(map.get("mark"), "-1");
		if ("1".equals(mark)) {
			List<Map> result = QBuilder.db("basedb").select("new map(vc.id as category_id, vc.parent_id as parent_id, vc.title as title, vc.sub_title as detail, count(vl.case_id) as num)").from("visit_category vc, visit_link vl").where(new Where("vc.parent_id =", id).and("vc.status = ", STATUS_ENABLE).and("vl.status = ", STATUS_ENABLE).and("vc.id = vl.cate_id")).group("vl.cate_id").query().getResultObj();
			map.put("sub", result);
		}
	}
    msg.put("data", surveys);
    Tools.encryptPrint(out, msg);
%>